﻿using System;
using System.Net;
using System.Threading.RateLimiting;
using Common;
using Extensions.Services.RateLimit.Config;
using Microsoft.AspNetCore.Http;
using static Org.BouncyCastle.Math.EC.ECCurve;

namespace Extensions.Services.RateLimit.Policys;
/// <summary>
/// 固定窗口
/// </summary>
public class FixedWindowLimiterPolicy:BaseLimiterPolicy
{
    protected override RateLimitPartition<IPAddress> RateLimit(IPAddress ipAddress)
    {
        return RateLimitPartition.GetFixedWindowLimiter(ipAddress, _ => new FixedWindowRateLimiterOptions()
        {
            // 每次允许的 数量
            PermitLimit = base.Config.PermitLimit,
            // 窗口的时间
            Window = TimeSpan.FromSeconds(base.Config.Window),
            //队列处理顺序  老的数据先处理
            QueueProcessingOrder = QueueProcessingOrder.OldestFirst,
            // 等待队列的长度
            QueueLimit = base.Config.QueueLimit
        });
    }
}